package p2pcdnsim.cdn;

import java.util.List;
import java.util.Random;

import p2pcdnsim.exception.SurrogateAlreadyInsertedException;
import p2pcdnsim.network.RouterNode;


public class RandomPlacement implements PlacementStrategy{
	
	
	@Override
	public void placeSurrogates(List<Surrogate> surrogates,	CDN cdn) throws SurrogateAlreadyInsertedException{
		
		Random random = new Random();
		RouterNode[] nodes = cdn.getTopology().getNodes();
		
		for(Surrogate surrogate : surrogates) {
			RouterNode node;
			//find a node which has no surrogate
			while((node = nodes[random.nextInt(nodes.length)]).getNetworkLayer().getTransportLayer().getApplicationLayer().getApplicationProcess() != null);
			//Place surrogate
			cdn.placeSurrogate(surrogate,node);
		}		
	}

}
